﻿///////////////////////////////////////////////////////
// Prompt 1.0
///////////////////////////////////////////////////////

Type.registerNamespace('AfterEdge.Web.UI');

///////////////////////////////////////////////////////
// Enums
///////////////////////////////////////////////////////

AfterEdge.Web.UI.PostBackTarget = function() {};
AfterEdge.Web.UI.PostBackTarget.prototype = 
{
    Prompt: 0,
    Trigger: 1
}
AfterEdge.Web.UI.PostBackTarget.registerEnum('AfterEdge.Web.UI.PostBackTarget');

AfterEdge.Web.UI.TriggerControlType = function() {};
AfterEdge.Web.UI.TriggerControlType.prototype = 
{
    HtmlControl: 0,
    WebControl: 1,
    PageControl: 2
}
AfterEdge.Web.UI.TriggerControlType.registerEnum('AfterEdge.Web.UI.TriggerControlType');

///////////////////////////////////////////////////////
// Prompt Class
///////////////////////////////////////////////////////

AfterEdge.Web.UI.Prompt = function(element)
{
    AfterEdge.Web.UI.Prompt.initializeBase(this, [element]);
    this._title = '';
    this._message = null;
    this._titleLabel = null;
    this._messageLabel = null;
    this._body = null;
    this._handle = null;
    this._validationGroup = null;
    this._causesValidation = false;
    this._postBackUrl = null;
    this._postBackTarget = null;
    this._trigger = null;
    this._modal = null;
    this._allowDrag = null;
    this._backgroundCssClass = null;
    this._isShowing = false;
    
    this._background = null;
    this._iframe = null;
    
    this._buttons = [];
    this._triggers = [];
    
    // Used for legacy IE 6 support
    this._dropDowns = [];
    
    this._onMouseUpHandler = null;
    this._event = null;
    this._dragExtender = null;
    this._pageLoadEventHandler = null;
}

AfterEdge.Web.UI.Prompt.prototype = 
{
    initialize: function()
    {
        AfterEdge.Web.UI.Prompt.callBaseMethod(this, 'initialize');
        
        window[this.get_id()] = this;
        var baseElementID = this.get_element().id;
       
        this.set_visibilityMode(Sys.UI.VisibilityMode.collapse);
        this.hide();
        
        this.get_element().style.zIndex = 99999;
        
        // Find references to all elements we will need
        this._body = $get(String.format('{0}_Body', baseElementID));
        this._titleLabel = $get(String.format('{0}_Title', baseElementID));
        this._messageLabel = $get(String.format('{0}_Message', baseElementID));
        this._handle = $get(String.format('{0}_Handle', baseElementID));
        
        // Wire up client side page load to avoid operation aborted error
        this._pageLoadEventHandler = Function.createDelegate(this, this._onPageLoad);
        Sys.Application.add_load(this._pageLoadEventHandler);
                       
        if(this._allowDrag === true && this._handle)
        {
            this._handle.style.cursor = 'move'; 
            
            // Need to capture the move event on the 
            // drag extender to realign the iFrame when in IE 6.
            this._dragExtender = $find(this.get_id() + '_dragExtender');
            this._onDragCompleteHandler = Function.createDelegate(this, this._onDrag);
            this._dragExtender.add_move(this._onDragCompleteHandler);
        }     
            
        // Add handler for scroll / resize to adjust prompt / background positions.
        this._onresizeHandler = Function.createDelegate(this, this._onResize);
        $addHandler(window, 'resize', this._onresizeHandler);
        $addHandler(window, 'scroll', this._onresizeHandler);      
    },
    
    dispose: function()
    {
        $clearHandlers(this.get_element());
        $removeHandler(window, 'resize', this._onresizeHandler);
        $removeHandler(window, 'scroll', this._onresizeHandler);  
        this._onresizeHandler = null;
        
        if(this._allowDrag === true && this._handle)
        {
            this._dragExtender.remove_move(this._onDragCompleteHandler);
            this._onDragCompleteHandler = null;
        }
        
        AfterEdge.Web.UI.Prompt.callBaseMethod(this, 'dispose');
    },
    
    // Events

    add_show: function(handler)
    {
        this.get_events().addHandler('show', handler);
    },
    
    remove_show: function(handler)
    {
        this.get_events().removeHandler('show', handler);
    },
    
    add_hide: function(handler)
    {
        this.get_events().addHandler('hide', handler);
    },
    
    remove_hide: function(handler)
    {
        this.get_events().removeHandler('hide', handler);
    },
    
    add_buttonSelect: function(handler)
    {
        this.get_events().addHandler('buttonSelect', handler);
    },
    
    remove_buttonSelect: function(handler)
    {
        this.get_events().removeHandler('buttonSelect', handler);
    }, 

    // Event Delegates
    
    _onPageLoad: function(sender, args)
    {
        var inAsyncPostback = Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();
        // The following calls modify the document.body, therefore then can not be called
        // until the initial DOM has been loaded and parsed.
        if(!inAsyncPostback && this._modal === true)
            window.setTimeout('window.' + this.get_id() + '._initializeModalBackground()', 1000);   
            
        if(!inAsyncPostback && Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
            window.setTimeout('window.' + this.get_id() + '._initializeIFrame()', 1000);
    },

    _onShow: function(e)
    {
        var handler = this.get_events().getHandler('show');
        if(handler)
        {
            handler(this, e);
        }
    },
    
    _onHide: function(e)
    {
        var handler = this.get_events().getHandler('hide');
        if(handler)
        {
            handler(this, e);
        }        
    },
    
    _onButtonSelect: function(e)
    {
        var handler = this.get_events().getHandler('buttonSelect');
        if(handler)
        {
            handler(this, e);
        }
    },
      
    _onResize: function(e)
    {
        if(this.get_visible() === true)
        {
            this._layout();
            this._layout();
        }
    },
    
    _onDrag: function(sender, args)
    {
        this._alignIFrame();
    },
        
    // Control Properties
    
    get_title: function()
    {
        return this._title;
    },
    
    set_title: function(value)
    {
        if(this._title !== value && value !== null)
        {
            this._title = value;
            this.raisePropertyChanged('title');
        }
    },
    
    get_message: function()
    {
        return this._message;
    },
    
    set_message: function(value)
    {
        if(this._message !== value)
        {
            this._message = value;
            this.raisePropertyChanged('message');
            
            if(!this.get_isUpdating())
                this.endUpdate();
        }
    },
        
    get_validationGroup: function()
    {
        return this._validationGroup;
    },
    
    set_validationGroup: function(value)
    {
        this._validationGroup = value;
    },
    
    get_causesValidation: function()
    {
        return this._causesValidation;
    },
    
    set_causesValidation: function(value)
    {
        this._causesValidation = value;
    },
    
    get_postBackUrl: function()
    {
        return this._postBackUrl;
    },
    
    set_postBackUrl: function(value)
    {
        this._postBackUrl = value;
    },
       
    get_postBackTarget: function()
    {
        return this._postBackTarget;
    },
    
    set_postBackTarget: function(value)
    {
        this._postBackTarget = value;
    },
    
    get_modal: function()
    {
        return this._modal;
    },
    
    set_modal: function(value)
    {
        this._modal = value;
    },
    
    get_allowDrag: function()
    {
        return this._allowDrag;
    },
    
    set_allowDrag: function(value)
    {
        this._allowDrag = value;
    },
    
    get_backgroundCssClass: function()
    {
        return this._backgroundCssClass;
    },
    
    set_backgroundCssClass: function(value)
    {
        this._backgroundCssClass = value;
    },
    
    get_buttons: function()
    {
        return this._buttons;
    },
    
    get_triggers: function()
    {
        return this._triggers;
    },
    
    get_isShowing: function()
    {
        return this._isShowing;
    },
    
    updated: function()
    {
        AfterEdge.Web.UI.Prompt.callBaseMethod(this, 'updated');
        
        this._messageLabel.innerHTML = this.get_message();
        this._titleLabel.innerHTML = this.get_title();
    },
    
    _initializeModalBackground: function()
    {
        // We only need 1 modal background.  The prompts can then share it.
        var modalBackground = $get('prompt_modal_background');
        if(!modalBackground)
        {
            modalBackground = document.createElement('div');
            modalBackground.id = 'prompt_modal_background';
            modalBackground.style.display = 'none';
            modalBackground.style.position = 'absolute';
            modalBackground.style.left = '0px';
            modalBackground.style.top = '0px';
            modalBackground.style.zIndex = this.get_element().style.zIndex - 1;
            modalBackground.className = this._backgroundCssClass;
            
            document.body.appendChild(modalBackground);
        }
        
        this._background = modalBackground;        
    },
    
    _initializeIFrame: function()
    {
        // We only need 1 iframe.  The prompts can then share it.
        var iFrame = $get('ie6Frame');
        if(!iFrame)
        {
            iFrame = document.createElement('iframe');
            iFrame.id = 'ie6Frame';
            iFrame.style.display = 'none';
            iFrame.style.position = 'absolute';
            iFrame.style.filter = 'alpha(style=0,opacity=0)';
            iFrame.style.zIndex = this.get_element().style.zIndex - 1;
            
            document.body.appendChild(iFrame);
        }
        this._iframe = iFrame;
    },
    
    _showModalBackground: function()
    {
        if(!this._background && this._modal === true)
            this._initializeModalBackground();  
                    
        if(this._background && this._background.style)
        {
            this._background.style.display = '';
        }
    },
    
    _hideModalBackground: function()
    {
        if(this._background && this._background.style)
            this._background.style.display = 'none';
    },
    
    _showIFrame: function()
    {
        if(!this._iframe && (Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version < 7))
            this._initializeIFrame();
        
        if(this._iframe && this._iframe.style)
            this._iframe.style.display = '';
    },
    
    _hideIFrame: function()
    {
        if(this._iframe && this._iframe.style)
            this._iframe.style.display = 'none';
    },
    
    _showPrompt: function()
    {
        this.set_visible(true);
        this._setDefaultFocus();
    },
    
    _layout: function()
    {
        var clientBounds = CommonToolkitScripts.getClientBounds();

        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
        
        var promptElement = this.get_element();
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
       
        // Defect 8364.  Resize the prompt and set scrolling
        // when the prompt is taller than the page.
        if(promptElement.offsetHeight >= clientHeight)
        {
            this._body.style.height = parseInt(clientHeight / 2) + 'px';
            if(typeof(this._body.style.overflowY) != 'undefined')
                this._body.style.overflowY = 'scroll';
            else
                this._body.style.overflow = 'scroll';
        }

        var leftPosition = parseInt(scrollLeft + ((clientWidth-promptElement.offsetWidth)/2));
        var topPosition = parseInt(scrollTop + ((clientHeight-promptElement.offsetHeight)/4));

        if(this._background && this._background.style)
        {
            this._background.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
            this._background.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';                       
        }
        
        Sys.UI.DomElement.setLocation(this.get_element(), leftPosition, topPosition);
        this._alignIFrame();
    },
    
    _alignIFrame: function()
    {
        // Used for IE 6 support to show div over windows object(ie dropdown menus).
        if(this._iframe && this._iframe.style)
        {
            var bounds = CommonToolkitScripts.getBounds(this.get_element());            

            this._iframe.style.width = bounds.width;
            this._iframe.style.height = bounds.height;
            
            Sys.UI.DomElement.setLocation(this._iframe, bounds.x, bounds.y);
        }
    },
    
    _getDropDowns: function()
    {
        if(Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
        {
            var dropDowns = document.getElementsByTagName('SELECT');
            for(var i = 0; i < dropDowns.length; i++)
            {
                if(!dropDowns[i].disabled)
                {
                    Array.add(this._dropDowns, dropDowns[i]);
                }
            }
        }
    },
    
    _toggleDropDowns: function(disabled)
    {
        for(var i = 0; i < this._dropDowns.length; i++)
        {
            this._dropDowns[i].disabled = disabled;
        }
    },
        
    show: function(e)
    {
        this._event = e;
        
        if(!this._isPageValid())
            return;
        
        var eventArgs = new Sys.CancelEventArgs();
        this._onShow(eventArgs);
        
        if(!eventArgs.get_cancel())
        {           
            if(this._modal === true)
            {
                this._showModalBackground();
                this._getDropDowns();
                this._toggleDropDowns(true);
            }
            this._showIFrame();
            this._showPrompt();
            this._layout();
            this._isShowing = true;
        }
    },
    
    hide: function()
    {
        var eventArgs = new Sys.CancelEventArgs();
        this._onHide(eventArgs);
        
        if(!eventArgs.get_cancel())
        {
            if(this._modal === true)
            {
                this._hideModalBackground();
                this._toggleDropDowns(false);
            }
            
            this._hideIFrame();
            this.set_visible(false);
            this._isShowing = false;
        }        
    },
    
    postBack: function(eventArgument)
    {
        if(this._postBackTarget === AfterEdge.Web.UI.PostBackTarget.Trigger)
        {
            if(this._trigger)
            {
                this._trigger.invokeHandler(this._event);
                return;
            }
        }
        
        var eventTarget = this.get_id().replace(/_/g, '$');
        
        if(this.get_causesValidation() === true || this.get_postBackUrl() !== null)
        {
            var options = new WebForm_PostBackOptions(eventTarget, eventArgument, this.get_causesValidation(), this.get_validationGroup(), this.get_postBackUrl(), false, true);
            WebForm_DoPostBackWithOptions(options);
        }
        else
        {
            __doPostBack(eventTarget, eventArgument);
        }
    },
      
    _setDefaultFocus: function()
    {
        this._buttons[0].get_element().focus();
    },
    
    _isPageValid: function()
    {
        if(this._causesValidation && typeof(Page_ClientValidate) === 'function')
        {
            return Page_ClientValidate(this._validationGroup);
        }
        return true;
    }    
}

AfterEdge.Web.UI.Prompt.registerClass('AfterEdge.Web.UI.Prompt', Sys.UI.Control, Sys.IDisposable);

///////////////////////////////////////////////////////
// PromptButton Class
///////////////////////////////////////////////////////

AfterEdge.Web.UI.PromptButton = function(element)
{
    AfterEdge.Web.UI.PromptButton.initializeBase(this, [element]);
    
    this._text = null;
    this._onClientClick = null;
    this._prompt = null;
    this._autoPostBack = null;
    this._commandArgument = null;
}

AfterEdge.Web.UI.PromptButton.prototype = 
{
    initialize: function()
    {
        AfterEdge.Web.UI.PromptButton.callBaseMethod(this, 'initialize');      
        
        this._onclickHandler = Function.createDelegate(this, this._onClick);
        $addHandler(this.get_element(), 'click', this._onclickHandler);        
    },
    
    dispose: function()
    {
        $clearHandlers(this.get_element());
        AfterEdge.Web.UI.PromptButton.callBaseMethod(this, 'dispose');
    },
    
    // Events Delegates
    
    _onClick: function(e)
    {
        var prompt = this.get_prompt();
        prompt.hide();
        
        // Calls PromptButton's onclick method.
        eval(this._onClientClick);
        
        // Trigger Prompt's select event
        var eventArgs = new AfterEdge.Web.UI.PromptSelectEventArgs(this);
        prompt._onButtonSelect(eventArgs);
              
        // Post the page if button is set to AutoPostBack
        if(this.get_autoPostBack() === true)
        {
            prompt.postBack(this.get_commandArgument());
        }        
    },
        
    // Properties
    
    get_text: function()
    {
        return this._text;
    },
    
    set_text: function(value)
    {
        this._text = value;
    },
    
    get_onClientClick: function()
    {
        return this._onClientClick;
    },
    
    set_onClientClick: function(value)
    {
        this._onClientClick = value;
    },
    
    get_autoPostBack: function()
    {
        return this._autoPostBack;
    },
    
    set_autoPostBack: function(value)
    {
        this._autoPostBack = value;
    },
    
    get_commandArgument: function()
    {
        return this._commandArgument;
    },
    
    set_commandArgument: function(value)
    {
        this._commandArgument = value;
    },
    
    get_prompt: function()
    {
        return this._prompt;
    },
    
    set_prompt: function(value)
    {
        this._prompt = value;
        Array.add(this._prompt.get_buttons(), this); 
    },
    
    // Methods
    
    updated: function()
    {
        AfterEdge.Web.UI.PromptButton.callBaseMethod(this, 'updated');
        
        this.get_element().value = this.get_text();
    }    
}

AfterEdge.Web.UI.PromptButton.registerClass('AfterEdge.Web.UI.PromptButton', Sys.UI.Control, Sys.IDisposable);

///////////////////////////////////////////////////////
// PromptExtender Class
///////////////////////////////////////////////////////

AfterEdge.Web.UI.PromptExtender = function(element)
{
    AfterEdge.Web.UI.PromptExtender.initializeBase(this, [element]);
    
    this._eventName = null;
    this._prompt = null;  
    this._component = null;
    this._controlHandler = null;
    this._controlType = null;
    this._preventDefault = false;
    this._handler = null;
    this._enabled = true;
}

AfterEdge.Web.UI.PromptExtender.prototype = 
{
    initialize: function()
    {
        AfterEdge.Web.UI.PromptExtender.callBaseMethod(this, 'initialize');
                
        this._syncHandlers();        
    },
    
    dispose: function()
    {
        $clearHandlers(this.get_element());
        AfterEdge.Web.UI.PromptExtender.callBaseMethod(this, 'dispose');
    },
    
    // Events
    
    add_condition: function(handler)
    {
        this.get_events().addHandler('condition', handler);
    },
    
    remove_condition: function(handler)
    {
        this.get_events().removeHandler('condition', handler);
    },
    
    // Event Delegates
    
    _onEvent: function(e)
    {
        if(!this._enabled)
            return;
        
        var eventArgs = new Sys.CancelEventArgs();
        this._onCondition(eventArgs)

        if(!eventArgs.get_cancel())
        {
            var prompt = this.get_prompt();
            prompt._trigger = this;
            prompt.show(e);
            
            if(this.get_preventDefault() === true)
                e.preventDefault();
        } 
        else 
        {
            this.invokeHandler(e);
        }           
    },
    
    _onCondition: function(e)
    {
        var handler = this.get_events().getHandler('condition');

        if(handler)
        {
            handler(this, e);
        }
    },
    
    // Properties
    
    get_eventName: function()
    {
        return this._eventName;
    },
    
    set_eventName: function(value)
    {
        this._eventName = value;
    },
    
    get_preventDefault: function()
    {
        return this._preventDefault;
    },
    
    set_preventDefault: function(value)
    {
        this._preventDefault = value;
    },
    
    get_prompt: function()
    {
        return this._prompt;
    }, 
    
    set_prompt: function(value)
    {
        this._prompt = value;
        Array.add(this._prompt.get_triggers(), this);
    },
    
    get_component: function()
    {
        return this._component;
    },
    
    set_component: function(value)
    {
        this._component = value;
    },
    
    get_controlType: function()
    {
        return this._controlType;
    },
    
    set_controlType: function(value)
    {
        this._controlType = value;
    },
    
    // Methods
    
    _syncHandlers: function()
    {
        this.clearControlHandler();        

        this._handler = Function.createDelegate(this, this._onEvent);

        $addHandler(this.get_element(), this.get_eventName(), this._handler);
    },
    
    clearControlHandler: function()
    {
        this._controlHandler = eval('this.get_element().on' + this.get_eventName());
        eval('this.get_element().on' + this.get_eventName() + ' = null')
    },
    
    _simulateEvent: function() 
    {
        var element = this.get_element();
        var eventName = this.get_eventName();
               
        // Call the original event handler
        if(typeof(this._controlHandler) === 'function')
            this._controlHandler.apply(element);
        
        // Raise the event
        var eventHandler = eval('element.' + eventName);
        if(typeof(eventHandler) !== 'undefined')
        {
            eval('element.' + eventName + '()');
        }
        else
        {    
            if(document.createEvent)
            {
                var evt = document.createEvent('UIEvents');
                evt.initUIEvent(eventName, true, true, window, 1);
                element.dispatchEvent(evt);
            }
            
            // Special case for linking a prompt to a standard hyperlink.
            // Invoking handler on hyperlink should call the hyperlinks href.
            if(eventName === 'click' && element.tagName === 'A')
            {
                if(element.target.trim() === '')
                    document.location = element.href;
                else
                    window.open(element.href, element.target);
            }
        }
    },
    
    invokeHandler: function(e)
    {
        this._enabled = false;
        this._simulateEvent();
        this._enabled = true;
    }
}

AfterEdge.Web.UI.PromptExtender.registerClass('AfterEdge.Web.UI.PromptExtender', Sys.UI.Behavior, Sys.IDisposable);

///////////////////////////////////////////////////////
// PromptElementEventExtender Class
///////////////////////////////////////////////////////

AfterEdge.Web.UI.PromptElementEventExtender = function()
{
    AfterEdge.Web.UI.PromptElementEventExtender.initializeBase(this);
    
    this._eventName = null;
    this._prompt = null;  
    this._handler = null;
}

AfterEdge.Web.UI.PromptElementEventExtender.prototype = 
{
    initialize: function()
    {
        AfterEdge.Web.UI.PromptElementEventExtender.callBaseMethod(this, 'initialize');
        
        this._handler = Function.createDelegate(this, this._onEvent);
        if(this._eventName === 'load')
        {
            Sys.Application.add_load(this._handler);
        }
        else if(this._eventName === 'unload')
        {
            Sys.Application.add_unload(this._handler);
        }
        else
        {
            $addHandler(window, this._eventName, handler);
        }        
    },
    
    dispose: function()
    {
        if(this._eventName === 'load')
        {
            Sys.Application.remove_load(this._handler);
        }
        else if(this._eventName === 'unload')
        {
            Sys.Application.remove_unload(this._handler);
        }
        else
        {
            $removeHandler(window, this._eventName, this._handler);
        }        
        
        this._handler = null;

        AfterEdge.Web.UI.PromptElementEventExtender.callBaseMethod(this, 'dispose');
    },
      
    // Events
    
    add_condition: function(handler)
    {
        this.get_events().addHandler('condition', handler);
    },
    
    remove_condition: function(handler)
    {
        this.get_events().removeHandler('condition', handler);
    },
    
    // Event Delegates
    
    _onEvent: function(e)
    {
        var eventArgs = new Sys.CancelEventArgs();
        this._onCondition(eventArgs)
        
        if(!eventArgs.get_cancel())
        {
            var prompt = this.get_prompt();
            prompt._trigger = this;
            prompt.show();
        } 
    },
    
    _onCondition: function(e)
    {
        var handler = this.get_events().getHandler('condition');

        if(handler)
        {
            handler(this, e);
        }
    },
    
    // Properties

    get_eventName: function()
    {
        return this._eventName;
    },
    
    set_eventName: function(value)
    {
        this._eventName = value;
    },
    
    get_prompt: function()
    {
        return this._prompt;
    }, 
    
    set_prompt: function(value)
    {
        this._prompt = value;
        Array.add(this._prompt.get_triggers(), this);
    }
}

AfterEdge.Web.UI.PromptElementEventExtender.registerClass('AfterEdge.Web.UI.PromptElementEventExtender', Sys.Component);

///////////////////////////////////////////////////////
// PromptSelectEventArgs Class
///////////////////////////////////////////////////////

AfterEdge.Web.UI.PromptSelectEventArgs = function(button)
{
    AfterEdge.Web.UI.PromptSelectEventArgs.initializeBase(this);
    this._button = button;
}

AfterEdge.Web.UI.PromptSelectEventArgs.prototype = 
{    
    get_button: function()
    {
        return this._button;
    }
}

AfterEdge.Web.UI.PromptSelectEventArgs.registerClass('AfterEdge.Web.UI.PromptSelectEventArgs', Sys.EventArgs);